#import "AppDelegate.h"

@implementation AppDelegate

- (id)init;
{
    if (![super init])
        return nil;
    averager = [[ImageAverage alloc] init];
    threshold = (double) 8.0;
    [self observeApp];
    return self;
}

- (void)observeApp;
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationWillTerminate:)
                                                 name: NSApplicationWillTerminateNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationDidFinishLaunching:)
                                                 name: NSApplicationDidFinishLaunchingNotification
                                               object:nil];  
}

- (void)observePatchController;
{
    [patchController addObserver:self 
                      forKeyPath:@"patch.FlipperCount.value"
                         options:nil
                         context:nil];
    [patchController addObserver:self 
                      forKeyPath:@"patch.isMonochrome.value"
                         options:nil
                         context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context;
{
    if ([keyPath isEqualToString:@"patch.FlipperCount.value"]){
        [averager setImage:[patchController valueForKeyPath:@"patch.outImage.value"]];
        average = [averager average];
        NSLog(@"Average = %i", average);
        NSMutableArray* theArray;
        theArray = [NSMutableArray arrayWithObjects:@"/n_set", [NSNumber numberWithInt:1000], @"t_trig", [NSNumber numberWithInt:average], nil];
        //NSLog(@"Sending");
        [controller sendMsg:theArray];
        //[controller log];
    }
    
    if ([keyPath isEqualToString:@"patch.isMonochrome.value"]){
        NSLog(@"Monochrome Change");
        [averager setMonochrome:[[patchController valueForKeyPath:@"patch.isMonochrome.value"] boolValue]];
    }
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification;
{
    NSLog(@"applicationDidFinishLaunching");
    [self observePatchController];
    NSLog(@"Controller type = ");
    NSLog([controller className]);
    [controller sendMsg:[NSMutableArray arrayWithObjects:@"/b_allocRead", [NSNumber numberWithInt:1], @"/Volumes/Docs/Users/jonathan/Desktop/superConductorFugueAsPerformedMundial/trunk/sandBox/kryptonite/PerformanceSounds/fugueMonoRegular/wobbleyThingLong.wav.short.wav", nil]];
    //load a buffer
    [controller sendMsg:[NSMutableArray arrayWithObjects:@"/s_new", @"JSTestRetrigger", 
                                        [NSNumber numberWithInt:1000], //nodeID
                                        [NSNumber numberWithInt:0], //Add to head
                                        [NSNumber numberWithInt:0], //target
                                        nil]];
    //load the trigger synthdef (JSTestRetrigger)
}

- (void)setMinimum:(double)min;
{
    NSLog(@"setMinimum");
    threshold = min;
    [controller sendMsg:[NSMutableArray arrayWithObjects:@"/n_set", [NSNumber numberWithInt:1000], @"thresh", [NSNumber numberWithDouble:min], nil]];
    //set the threshold on SC
}
- (double)minimum;
{
    NSLog(@"get Minimum");
    return threshold;
}

- (void)applicationWillTerminate:(NSNotification *)notification;
{
    NSLog(@"applicationWillTerminate");
    [patchController removeObserver:self 
                         forKeyPath:@"patch.FlipperCount.value"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [controller sendMsg:[NSMutableArray arrayWithObjects:@"/quit", nil]];
    //quit SC
    [averager release];
}

- (IBAction)log:(id)sender;
{
    NSLog(@"BUTTON PUSHED");
}


@end